package com.wangzz.mallchat.common.common.domain.enums;

import cn.hutool.http.ContentType;
import cn.hutool.json.JSON;
import com.wangzz.mallchat.common.common.domain.vo.resp.ApiResult;
import com.wangzz.mallchat.utils.JsonUtils;
import lombok.AllArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@AllArgsConstructor
public enum HttpErrorEnum {
    UNAUTHORIZED(401, "登录失效，请重新登录");

    private Integer errorCode;

    private String desc;

    public void errorResponse(HttpServletResponse response) throws IOException {
        // 封装异常信息返回401
        response.setStatus(errorCode);
        response.setContentType(ContentType.JSON.toString(StandardCharsets.UTF_8));
        // 返回具体对象
        response.getWriter().write(JsonUtils.toStr(ApiResult.fail(errorCode, desc)));
    }
}
